<?php

require_once __DIR__."/../constants.php";
require_once VENDOR_DIR."autoload.php";

use Sunra\PhpSimple\HtmlDomParser;
use Predis\Client;
use Ares333\Curl\Curl;

$urls = [
    'http://man.linuxde.net/par/1',
    'http://man.linuxde.net/par/1/page/2',
    'http://man.linuxde.net/par/1/page/3',

    'http://man.linuxde.net/par/2',
    'http://man.linuxde.net/par/2/page/2',
    'http://man.linuxde.net/par/3/page/3',

    'http://man.linuxde.net/par/3',
    'http://man.linuxde.net/par/3/page/2',
    'http://man.linuxde.net/par/3/page/3',

    'http://man.linuxde.net/par/4',
    'http://man.linuxde.net/par/4/page/2',
    'http://man.linuxde.net/par/4/page/3',

    'http://man.linuxde.net/par/5',
    'http://man.linuxde.net/par/5/page/2',
    'http://man.linuxde.net/par/5/page/3',
];

$cmdInfoList = [];
foreach ($urls as $url) {
    $cmdInfoList = array_merge($cmdInfoList, Processor::getCmdInfoList($url));
}
Processor::saveCmdInfoList($cmdInfoList);

class Processor{

    const HASH_CONTENT_LIST = "hash:content:list";

    protected static $redisInstance;

    public static function createRedis()
    {
        if(empty(self::$redisInstance)) {
            self::$redisInstance = new Client('tcp://127.0.0.1:6379');
        }
        return self::$redisInstance;
    }
    public static function getCmdInfoList($pageUrl)
    {
        $content = file_get_contents($pageUrl);
        $dom =  HtmlDomParser::str_get_html( $content);
        $category = trim($dom->find('div.title_list>.list_hd',0)->text());
        $contentArray = [];
        foreach ($dom->find('div.list_bd>.item') as $item) {
            $name = $item->find('div.name',0)->text();
            $contentArray[$name] = [
                'category'=>  $category,
                'name'=>$name,
                'url'=>$item->find("div.name>a",0)->href,
                'description'=>$item->find('div.des',0)->text(),
            ];
        }
        return $contentArray;
    }
    public static function saveCmdInfoList($cmdInfoList)
    {
        $curl = new Curl();
        $curl->opt = [
            CURLOPT_RETURNTRANSFER => true,
        ];
        $curl->maxThread = 20;
        foreach ($cmdInfoList as $cmdInfo) {
            //测试换行回车的切换
            //if($cmdInfo['name'] != 'awk') continue;
            $curl->add([
                'opt' => [
                    CURLOPT_URL => $cmdInfo['url'],
                ],
                'args' => [
                    'cmdInfo' => $cmdInfo
                ]
            ], function($r, $args){
                $dom =  HtmlDomParser::str_get_html( $r['body'],true,true,DEFAULT_TARGET_CHARSET,false);

                $post = $dom->find('div.post_bd',0);

                foreach ($post->find('img') as $img) {
                    $name = mt_rand(1000,99999)."_".basename($img->src);
                    self::downloadImage($img->src,$name);
                    $imgPath = "/static/images/".$name;
                    $img->setAttribute('src',$imgPath);
                }

                $content = trim($post->innertext());
                self::replaceAll($content);
                $cmdInfo = $args['cmdInfo'];
                $cmdInfo['content'] = $content;
                $cmdInfo['tag'] = trim($dom->find("div.post_hd>div.tag",0)->text());

                echo "Collecting url:".$cmdInfo['url']."\n";

                self::createRedis()->
                hsetnx(self::HASH_CONTENT_LIST,$cmdInfo['name'],
                    json_encode($cmdInfo,JSON_UNESCAPED_UNICODE));
            });
        }
        $curl->start();
    }
    public static function downloadImage($url,$name = "")
    {
        if(empty($name)) {
            $name = basename($url);
        }
        return file_put_contents(STATIC_DIR.'images/'.$name, file_get_contents($url));
    }
    public static function replaceAll(&$content)
    {
        $content = str_replace('http://man.linuxde.net',"",$content);
    }
}